$(function(){
	//导入脚部
	$('#login-foot').load('foot.html');
	//用户名失去焦点触发
	var name;
	var pass;
	$('#name').change(function(){
		name = $(this).val();	
		console.log(name);
		
		
if($.cookie(name)==null){
	//通过验证后运行的函数，里面要加上表单提交的函数，否则表单不会提交。				
	$.validator.setDefaults({
		//验证成功后执行函数
	    submitHandler: function() {
	      alert("提交成功!");
	      pass = $('#pass').val();
	      var uname = {name:name,pass:pass};
	      var arr = JSON.stringify(uname);
	      $.cookie(name,arr,{path: "/", expires: 7 })
	    
			console.log('加入')
			
	      //提交成功后重置表单
	       //validator.resetForm();
	    }
	});
	$().ready(function() {
		// 在键盘按下并释放及提交后验证提交表单
  		$("#form1").validate({
			rules: {
			         
			    name: {
			        required: true,
			        minlength: 4,
			        
			    },
			    pas: {
			        required: true,
			        rangelength:[6,20]
			    },
			    pass: {
			        required: true,
			        rangelength:[6,20],
			        equalTo: "#pas"
			    },
			    code:{
			    	required: true,				    	
			        equalTo: "#code1"
			    },
			    
			     //自定义验证提示信息 			      			      
			    messages: {
			   
				   	name: {
				        required: "请输入用户名",
				        minlength: "用户名必需由两个字母组成"
				    },
				    pas: {
				        required: "请输入密码",
				        minlength: "密码长度不能小于 5 个字母"
				    },
				    pass: {
				        required: "请输入密码",
				        minlength: "密码长度不能小于 5 个字母",
				        equalTo: "两次密码输入不一致"
				    },
				    code:{
				    	required: "请输入验证码",				       
				        equalTo: "验证码错误"
				    }
			    }
			}  
	   })
	});		
		}else{
			console.log('用户名已存在')
			
			var strl = $.cookie(name); 
       		o1 = JSON.parse(strl);　
			console.log(o1.name)
			console.log(o1.pass)
		}
		
	})

	//定义空字符串
	var str = "";  
	var code = function(){
		//每次加载页面都会先生成一个新的验证码						
					        
		for(var i=0;i<4;i++){   //获取4位验证码
						
			var num = parseInt(Math.random()*62);
			if(num>=0&&num<=9){  //获取0-9的数
				str+=num;
							
				}else if(num>=10&&num<=35){   // 获取大写字母
					str+= String.fromCharCode(num+55);
								
					}else if(num>=36&&num<=62){  //获取小写字母
						str+= String.fromCharCode(num+61)
														
					}
				
		}									
		$('#code1').val(str)
		console.log(str);
	};
	code();
		//点击更新验证码
	$('#code1').click(function(){
		str = ""; 
		code();
	});
	
	
})


